home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / AptUrl / gtk / GtkUI.py
Encoding:
Python Source  |  2009-04-29  |  7.4 KB  |  219 lines

  1.  
  2. import pygtk
  3. pygtk.require("2.0")
  4. import gtk
  5. import gtk.glade
  6. import gobject
  7.  
  8. import subprocess 
  9. import gettext
  10. from gettext import gettext as _
  11. from tempfile import NamedTemporaryFile
  12.  
  13. import os
  14. import os.path
  15.  
  16. import time
  17. import thread
  18. import apt_pkg
  19.  
  20.  
  21. from AptUrl.UI import AbstractUI
  22. from AptUrl import Helpers
  23.  
  24.  
  25. class GtkUI(AbstractUI):
  26.     def __init__(self):
  27.         gtk.init_check()
  28.         # create empty dialog
  29.         self.dia_xml = gtk.glade.XML('/usr/share/apturl/apturl.glade', 
  30.                                      'confirmation_dialog', 
  31.                                      "apturl")
  32.         self.dia = self.dia_xml.get_widget('confirmation_dialog')
  33.         self.dia.realize()
  34.  
  35.     # generic dialogs
  36.     def _get_dialog(self, dialog_type, summary, msg="", buttons=gtk.BUTTONS_CLOSE):
  37.         " internal helper for dialog construction "
  38.         d = gtk.MessageDialog(parent=self.dia,
  39.                               flags=gtk.DIALOG_MODAL,
  40.                               type=dialog_type,
  41.                               buttons=buttons)
  42.         d.set_title("")
  43.         d.set_markup("<big><b>%s</b></big>\n\n%s" % (summary, msg))
  44.         d.set_icon(gtk.icon_theme_get_default().load_icon('deb', 16, False))
  45.         d.set_keep_above(True)
  46.         d.realize()
  47.         d.window.set_functions(gtk.gdk.FUNC_MOVE)
  48.         return d
  49.         
  50.     def error(self, summary, msg=""):
  51.         d = self._get_dialog(gtk.MESSAGE_ERROR, summary, msg)
  52.         d.run()
  53.         d.destroy()
  54.         return False
  55.  
  56.     def message(self, summary, msg="", title=""):
  57.         d = self._get_dialog(gtk.MESSAGE_INFO, summary, msg)
  58.         d.set_title(title)
  59.         d.run()
  60.         d.destroy()
  61.         return True
  62.  
  63.     def yesNoQuestion(self, summary, msg, title="", default='no'):
  64.         d = self._get_dialog(gtk.MESSAGE_QUESTION, summary, msg,
  65.                              buttons=gtk.BUTTONS_YES_NO)
  66.         d.add_buttons
  67.         d.set_title(title)
  68.         res = d.run()
  69.         d.destroy()
  70.         if res != gtk.RESPONSE_YES:
  71.             return False
  72.         return True
  73.     
  74.     # specific dialogs
  75.     def doEnableSection(self, sections):
  76.         cmd = ["gksu", "--desktop",
  77.                "/usr/share/applications/gnome-app-install.desktop",
  78.                "--",
  79.                "gnome-app-install-helper",
  80.                "-e", "%s" % ' '.join(sections)]
  81.         try:
  82.             output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0]
  83.         except OSError, e:
  84.             print >>sys.stderr, "Execution failed:", e
  85.             return True
  86.         #FIXME: Very ugly, but gksu doesn't return the correct exit states
  87.         if not output.startswith("Enabled the "):
  88.             return False
  89.         return True
  90.  
  91.     
  92.     def doEnableChannel(self, channelpath, channelkey):
  93.         cmd = ["gksu",
  94.                "--desktop", "/usr/share/applications/gnome-app-install.desktop",
  95.                "--",
  96.                "install", "--mode=644","--owner=0",channelpath,
  97.                apt_pkg.Config.FindDir("Dir::Etc::sourceparts")]
  98.         res=subprocess.call(cmd)
  99.         if not res == 0:
  100.             return False
  101.         # install the key as well
  102.         if os.path.exists(channelkey):
  103.             cmd = ["gksu",
  104.                    "--desktop",
  105.                    "/usr/share/applications/gnome-app-install.desktop",
  106.                    "--",
  107.                    "apt-key", "add",channelkey]
  108.             res=subprocess.call(cmd)
  109.             if not res == 0:
  110.                 return False
  111.         return True
  112.  
  113.     def askInstallPackage(self, package, summary, description, homepage):
  114.         # populate the dialog
  115.         dia = self.dia
  116.         dia_xml = self.dia_xml
  117.         header = _("Install additional software?")
  118.         body = _("Do you want to install package '%s'?") % package
  119.         dia.set_keep_above(True)
  120.         dia.set_title('')
  121.         header_label = dia_xml.get_widget('header_label')
  122.         header_label.set_markup("<b><big>%s</big></b>" % header)
  123.         body_label = dia_xml.get_widget('body_label')
  124.         body_label.set_label(body)
  125.         description_text_view = dia_xml.get_widget('description_text_view')
  126.         tbuf = gtk.TextBuffer()
  127.         desc = "%s\n\n%s" % (summary, Helpers.format_description(description))
  128.         tbuf.set_text(desc)
  129.         description_text_view.set_buffer(tbuf)
  130.         dia.set_icon(gtk.icon_theme_get_default().load_icon('deb', 16, False))
  131.         
  132.         # check if another package manager is already running
  133.         # FIXME: just checking for the existance of the file is
  134.         #        not sufficient, it need to be tested if it can
  135.         #        be locked via apt_pkg.GetLock() 
  136.         #        - but that needs to run as root 
  137.         #        - a dbus helper might be the best answer here
  138.         #args = (update_button_status, dia_xml.get_widget("yes_button"),
  139.         #    dia_xml.get_widget("infolabel"))
  140.         #args[0](*args[1:])
  141.         #timer_id = gobject.timeout_add(750, *args )
  142.         
  143.         # show the dialog
  144.         res = dia.run()
  145.         #gobject.source_remove(timer_id)
  146.         if res != gtk.RESPONSE_YES:
  147.             dia.hide()
  148.             return False
  149.         
  150.         # don't set on-top while installing
  151.         dia.set_keep_above(False)
  152.         return True
  153.  
  154. #    def _update_button_status(button, label):
  155. #        if os.path.isfile("/var/lib/dpkg/lock"):
  156. #            button.set_sensitive(False)
  157. #            label.set_markup("<b>Another package manager is already running...</b>")
  158. #        else:
  159. #            button.set_sensitive(True)
  160. #            label.set_text("")
  161. #        return True
  162.  
  163.     # progress etc
  164.     def doUpdate(self):
  165.         p = subprocess.Popen(['gksu',
  166.                               '--desktop',
  167.                               '/usr/share/applications/synaptic.desktop',
  168.                               '--',
  169.                               '/usr/sbin/synaptic',
  170.                               '--hide-main-window',
  171.                               '--non-interactive',
  172.                               '--parent-window-id', str(self.dia.window.xid),
  173.                               '--update-at-startup',
  174.                               ])
  175.         self._wait_for_synaptic(p)
  176.  
  177.     def doInstall(self, apturl):
  178.         # run synaptic
  179.         temp = NamedTemporaryFile()
  180.         temp.write("%s\t install\n" % apturl.package)
  181.         temp.flush()
  182.         p = subprocess.Popen(['gksu',
  183.                               '--desktop',
  184.                               '/usr/share/applications/synaptic.desktop',
  185.                               '--',
  186.                               '/usr/sbin/synaptic',
  187.                               '--hide-main-window',
  188.                               '--non-interactive',
  189.                               '--parent-window-id', str(self.dia.window.xid),
  190.                               '--set-selections-file', temp.name
  191.                               ])
  192.         self._wait_for_synaptic(p)
  193.         temp.close()    
  194.  
  195.     # helpers
  196.     def _wait_for_p(self, p, lock):
  197.         " helper for the thread to wait for process p to finish "
  198.         p.wait()
  199.         lock.release()
  200.  
  201.     def _wait_for_synaptic(self, p):
  202.         # wait for synaptic
  203.         lock = thread.allocate_lock()
  204.         lock.acquire()
  205.         thread.start_new_thread(self._wait_for_p, (p, lock))
  206.  
  207.         self.dia.set_sensitive(False)
  208.         while lock.locked():
  209.             while gtk.events_pending():
  210.                 gtk.main_iteration()
  211.             time.sleep(0.01)
  212.         self.dia.set_sensitive(True)
  213.         return True
  214.  
  215.  
  216. if __name__ == "__main__":
  217.     ui = GtkUI()
  218.     ui.error("foo","bar")
  219.